<?php
// 1908080121李定邦、1902030115李龙烽
namespace app\admin\controller;
use app\BaseController;
use think\App;

class User extends BaseController
{
    private $_user;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->checkIsAdminLogin();
        $this->_user = app('db')->name('user');
    }

    /**
     * 用户列表
     * @return mixed
     */
    public function index() {
        $keyword = $this->request->get('keyword');
        $query   = $this->_user->where("u_nick", "LIKE", "%$keyword%");
        $data    = $query->paginate(10);
        $page    = $data->render();
        $data = [
            'list'     => $data,
            'page'     => $page,
        ];

        return app('view')->fetch('', $data);
    }

    /**
     * 编辑用户
     * @param string $unick
     * @return mixed
     */
    public function update($unick = '') {
        $info = $this->_user->where('u_nick', $unick)->find();
        if ($this->request->isPost()) {
            $u_pa    = $this->request->post('u_pa', '', 'trim');
            $u_email = $this->request->post('u_email', '', 'trim');
            $u_tel   = $this->request->post('u_tel', '', 'trim');
            $u_nick  = $this->request->post('u_nick', '', 'trim');
            $data    = [
                'u_nick'  => $u_nick,
            ];
            try {
                $file   = $this->request->file('u_img2');
                if ($file) {
                    $u_img = app('filesystem')->disk('portrait')->putFile('', $file, 'md5');
                    $data['u_img'] = $u_img;
                }
            } catch (\Exception $exception) {

            }
            !empty($u_pa) && $data['u_pa'] = md5($u_pa);
            !empty($u_email) && $data['u_email'] = $u_email;
            !empty($u_tel) && $data['u_tel']= $u_tel;
            $res = app('db')->name('user')->where('u_nick', $u_nick)->update($data);
            $res ? $this->success('更新成功', '/user/list') : $this->error('更新失败');
        } else {
            $data = [
                'info' => $info,
            ];
            return app('view')->fetch('', $data);
        }
    }

}